const app = getApp()
const WXAPI = require("../../../api/newApi")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgHttp: app.globalData.imgHttp, //图片前缀
    useShow: false, //用卡弹窗
    projectLs: [], //计次卡核销项目
    resultObject: {},
    customerCardId: '',
    payType: '',
    info: {},
    propShow: false, //多项计费卡核销弹窗
    assetId: '', //
    isShowErCode: false, ////是否弹出二维码核销（在计次卡、课时卡购买成功后，用户点击用卡时弹出）
    codeInformation: {}, //二维码信息
    subscribeShow: true, //订阅消息弹窗
    tmplIds: [], //订阅消息数组
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      statusHeight: app.gettop.height,
      status: app.gettop.status,
      navHeight: app.gettop.navHeight,
      mobbleBottom: app.mobile.mobbleBottom,
    })
    this.data.customerCardId = options.customerCardId
    this.data.assetId = options.assetId
    var payType = options.payType
    if (payType == 'tuangou') {
      this.getDycardInfo()
    } else {
      this.getcardInfo()
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 查询订阅模版id
  getSubscriptionLs() {
    var data = {
      paySource: app.globalData.paySource,
      cardType: this.data.info.cardType
    }
    WXAPI.subscriptionLs(data, (res) => {
      if (res.code == 200) {
        this.setData({
          tmplIds: res.result
        })
      }
    })
  },
  getcardInfo() { //查询购卡成功信息
    const that = this
    // this.data.customerCardId = '1540176839254376450'
    var customerCardId = this.data.customerCardId
    var url = `/app/v1/asset/card/buy/succeed/${customerCardId}`
    WXAPI.succeedCardinfo(url, '', (res) => {
      this.data.info = res.result
      if (res.code === 200) {
        that.setData({
          info: this.data.info,
        })
        this.getSubscriptionLs()
      }
    })
  },
  getDycardInfo() { //查询购卡成功信息
    const that = this
    // this.data.customerCardId = '1540173579307089922' 
    var customerCardId = this.data.customerCardId
    var url = `/app/v1/asset/card/groupon/buy/succeed/${customerCardId}`
    WXAPI.succeedDyCardinfo(url, '', (res) => {
      this.data.info = res.result
      if (res.code === 200) {
        that.setData({
          info: this.data.info,
        })
        this.getSubscriptionLs()
      }
    })
  },
  getUse(e) { //用会员卡
    if (this.data.info.cardType == 2 || this.data.info.cardType == 4 || this.data.info.cardType == 9) { //计次卡、课时卡、计时卡逻辑
      wx.navigateTo({
        url: '/ecCanvas/pages/myShareholderCard/otherCarddetail/prepaidCode/prepaidCode?customerCardId=' + this.data.info.customerCardId,
      })
    } else {
      wx.showLoading({
        title: '加载中',
      })
      let customerCardId = this.data.customerCardId
      var data = {
        customerCardId: customerCardId
      }
      WXAPI.verificationCardInfo(data, (res) => {
        if (res.code == 200) {
          this.data.resultObject = res.result
          if (res.result.cardType == 5) {
            this.data.projectLs = res.result.appCustomerCardExperienceDetailDTOList
          } else if (res.result.cardType == 8) {
            wx.reLaunch({
              url: '/userPageC/pages/abulkOrderDetail/abulkOrderDetail?customerCardId=' + res.result.customerCardId,
            })
          } else {
            this.data.projectLs = res.result.appCustomerCardNumberDetailDTOList
          }
          console.log(this.data.projectLs)
          wx.hideLoading()
          if (res.result.qrCode != '') { //多项目计费卡
            wx.navigateTo({
              url: '/ecCanvas/pages/myShareholderCard/otherCarddetail/prepaidCode/prepaidCode?customerCardId=' + res.result.customerCardId,
            })
          } else {
            this.setData({
              useShow: true,
              projectLs: this.data.projectLs,
              resultObject: this.data.resultObject
            })
          }
        }
      })
      setTimeout(() => {
        wx.hideLoading()
      }, 6000)
    }
  },
  propFalse() {
    this.setData({
      propShow: false,
      isShowErCode: false,
    })
  },
  onScuess(e) { //用卡成功
    console.log(e)
    let detail = JSON.stringify(e.detail)
    wx.redirectTo({
      url: './puchSuccess/puchSuccess?detail=' + detail + '&cardType=' + this.data.info.cardType,
    })
  },
  // 查看详情
  jumpCardDetail() {
    wx.navigateTo({
      url: '/ecCanvas/pages/myShareholderCard/otherCarddetail/otherCarddetail?assetId=' + this.data.assetId + '&id=' + this.data.customerCardId,
    })
  },
  handleChose() { //关闭页面
    wx.reLaunch({
      // url: '/pages/abulkPage/abulkPage',
      url: '/pages/userMarket/userMarket',
    })
  },
})